#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    UI_init();

    connect(ui->pushButton,SIGNAL(clicked()),SLOT(pushButton_pressed()));

    connect(ui->pushButton_2,SIGNAL(clicked()),SLOT(pushButton_2_pressed()));

    connect(ui->pushButton_3,SIGNAL(clicked()),SLOT(pushButton_3_pressed()));

    connect(ui->pushButton_4,SIGNAL(clicked()),SLOT(pushButton_4_pressed()));

    connect(ui->pushButton_5,SIGNAL(clicked()),SLOT(pushButton_5_pressed()));

    connect(ui->pushButton_6,SIGNAL(clicked()),SLOT(pushButton_6_pressed()));

    connect(ui->pushButton_7,SIGNAL(clicked()),SLOT(pushButton_7_pressed()));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::UI_init()
{
    QImage image;
    image_url = createImageUrl(i);
    image.load(image_url);
    ui->label_2->setPixmap(QPixmap::fromImage(image));
    ui->lineEdit->setText(QString("%1").arg(Num[i]));
    ui->label_4->setText(QString("%1元").arg(Price[i]));
    i++;
    image_url = createImageUrl(i);
    image.load(image_url);
    ui->label_3->setPixmap(QPixmap::fromImage(image));
    ui->lineEdit_2->setText(QString("%1").arg(Num[i]));
    ui->label_5->setText(QString("%1元").arg(Price[i]));

    ui->pushButton_7->hide();

    beep = new Beep();
}

QString MainWindow::createImageUrl(int i)
{
    QString image_url = QString(":/image/0%1.jpg").arg(i);
    return image_url;
}

void MainWindow::pushButton_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    if(Num[i-1] > 0)
    {
        Num[i-1] -- ;
        ui->lineEdit->setText(QString("%1").arg(Num[i-1]));
    }
}

void MainWindow::pushButton_2_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    Num[i-1] ++ ;
    ui->lineEdit->setText(QString("%1").arg(Num[i-1]));
}

void MainWindow::pushButton_3_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    if(Num[i] > 0)
    {
        Num[i] -- ;
        ui->lineEdit_2->setText(QString("%1").arg(Num[i]));
    }
}

void MainWindow::pushButton_4_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    Num[i] ++ ;
    ui->lineEdit_2->setText(QString("%1").arg(Num[i]));
}

void MainWindow::pushButton_5_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    if(i <= 4)
    {
        QImage image;
        ui->pushButton_7->show();
        i++;
        image_url = createImageUrl(i);
        image.load(image_url);
        ui->label_2->setPixmap(QPixmap::fromImage(image));
        ui->lineEdit->setText(QString("%1").arg(Num[i]));
        ui->label_4->setText(QString("%1元").arg(Price[i]));
        i++;
        image_url = createImageUrl(i);
        image.load(image_url);
        ui->label_3->setPixmap(QPixmap::fromImage(image));
        ui->lineEdit_2->setText(QString("%1").arg(Num[i]));
        ui->label_5->setText(QString("%1元").arg(Price[i]));
        if(i == 6)  ui->pushButton_5->hide();
    }
}
void MainWindow::pushButton_6_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    total = new Total(this,nullptr);    //将前一个窗口的指针传给后一个窗口，方便返回时可以显示原窗口
    this->hide();
    total->show();
}

void MainWindow::pushButton_7_pressed()
{
    beep->beepON();
    QThread::msleep(100);
    beep->beepOFF();
    if(i > 2)
    {
        int j;
        QImage image;
        ui->pushButton_5->show();
        i -= 2;
        if(i == 2)
        {
            ui->pushButton_7->hide();
        }
        image_url = createImageUrl(i);
        image.load(image_url);
        ui->label_3->setPixmap(QPixmap::fromImage(image));
        ui->lineEdit_2->setText(QString("%1").arg(Num[i]));
        ui->label_5->setText(QString("%1元").arg(Price[i]));
        j = i - 1;
        image_url = createImageUrl(j);
        image.load(image_url);
        ui->label_2->setPixmap(QPixmap::fromImage(image));
        ui->lineEdit->setText(QString("%1").arg(Num[j]));
        ui->label_4->setText(QString("%1元").arg(Price[j]));
    }
}

